/*
 * BlueMainWindow.java
 *
 * Created on 24. November 2008, 19:48
 */

package bcamone.bluetooth;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.bluetooth.*;
import java.util.Vector;

/**
 *
 * @author  Administrator
 */
public class BlueMainWindow extends javax.swing.JFrame {
    
    BlueConnectionManager BlueConnect;

    /** Creates new form BlueMainWindow */
    public BlueMainWindow() {
        initComponents();
        BlueConnect = new BlueConnectionManager();
        jLabelCheckHealth.setVisible(false);
        jLabelShowStack.setVisible(false);
        jLabelPowerError.setVisible(false);
        textAreaListDevices.setVisible(false);
    }
    
    public BlueMainWindow(BlueConnectionManager bcm) {
        initComponents();
        BlueConnect = bcm;
        jLabelCheckHealth.setVisible(false);
        jLabelShowStack.setVisible(false);
        jLabelPowerError.setVisible(false);
        textAreaListDevices.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonCheckHealth = new javax.swing.JButton();
        jLabelCheckHealth = new javax.swing.JLabel();
        jButtonShowStack = new javax.swing.JButton();
        jLabelShowStack = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jButtonListDevices = new javax.swing.JButton();
        textAreaListDevices = new java.awt.TextArea();
        jLabelPowerError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("BlueTest @ BCAMone");
        setBackground(new java.awt.Color(153, 255, 255));
        setIconImages(null);

        jButtonCheckHealth.setText("Check Health");
        jButtonCheckHealth.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCheckHealthMouseClicked(evt);
            }
        });

        jLabelCheckHealth.setBackground(new java.awt.Color(0, 255, 0));
        jLabelCheckHealth.setText("Health");

        jButtonShowStack.setText("Show Stack");
        jButtonShowStack.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonShowStackMouseClicked(evt);
            }
        });

        jLabelShowStack.setText("Stack");

        jButtonListDevices.setText("List Devices");
        jButtonListDevices.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonListDevicesMouseClicked(evt);
            }
        });

        textAreaListDevices.setEditable(false);

        jLabelPowerError.setText("power is off !!!");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jButtonCheckHealth)
                        .addGap(31, 31, 31)
                        .addComponent(jLabelCheckHealth)
                        .addGap(84, 84, 84)
                        .addComponent(jButtonShowStack)
                        .addGap(18, 18, 18)
                        .addComponent(jLabelShowStack))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 604, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonListDevices)
                            .addComponent(jLabelPowerError))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                        .addComponent(textAreaListDevices, javax.swing.GroupLayout.PREFERRED_SIZE, 439, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCheckHealth)
                    .addComponent(jLabelCheckHealth)
                    .addComponent(jButtonShowStack)
                    .addComponent(jLabelShowStack))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(157, 157, 157)
                        .addComponent(jButtonListDevices)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelPowerError))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(textAreaListDevices, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonCheckHealthMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCheckHealthMouseClicked
// TODO add your handling code here:
    boolean isOn = BlueConnect.blueIsOn();
    if (isOn) {
        jLabelCheckHealth.setText("ON");
    }
    else{
        jLabelCheckHealth.setText("OFF");
    }
    jLabelCheckHealth.setVisible(true);
}//GEN-LAST:event_jButtonCheckHealthMouseClicked

private void jButtonShowStackMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonShowStackMouseClicked
// TODO add your handling code here:
        boolean isOn = BlueConnect.blueIsOn();
    if (isOn) {
            try {
                jLabelShowStack.setText(BlueConnect.getBlueDeviceName());
            } catch (BluetoothStateException ex) {
                jLabelShowStack.setText("ERROR");
            }
    }
    else{
        jLabelShowStack.setText("NO STACK");
    }
    jLabelShowStack.setVisible(true);
}//GEN-LAST:event_jButtonShowStackMouseClicked

private void jButtonListDevicesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonListDevicesMouseClicked
// TODO add your handling code here:
    jLabelPowerError.setVisible(false);
    if (BlueConnect.blueIsOn())
    {
        Vector disVec = BlueConnect.getDeviceList();
        int disCount = disVec.size();
        textAreaListDevices.setVisible(true);
        if (disCount <=0)
        {
            textAreaListDevices.append("No device found");
        }
        else
        {
            textAreaListDevices.append("Count of found Bluetooth Devices: "+disCount);
            textAreaListDevices.append("\n");
            for (int i = 0; i <disCount; i++) 
            {
                    RemoteDevice remoteDevice=(RemoteDevice)disVec.elementAt(i);
                        try {
                            textAreaListDevices.append((i + 1) + ". " + remoteDevice.getBluetoothAddress() + " (" + remoteDevice.getFriendlyName(true) + ")");
                            textAreaListDevices.append("\n");
                        } catch (IOException ex) {
                            Logger.getLogger(BlueMainWindow.class.getName()).log(Level.SEVERE, null, ex);
                        }
            }
        }
    }
    else
    {
        jLabelPowerError.setVisible(true);
    }
    
}//GEN-LAST:event_jButtonListDevicesMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BlueMainWindow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCheckHealth;
    private javax.swing.JButton jButtonListDevices;
    private javax.swing.JButton jButtonShowStack;
    private javax.swing.JLabel jLabelCheckHealth;
    private javax.swing.JLabel jLabelPowerError;
    private javax.swing.JLabel jLabelShowStack;
    private javax.swing.JSeparator jSeparator1;
    private java.awt.TextArea textAreaListDevices;
    // End of variables declaration//GEN-END:variables

}
